import Popup from "../btoolkit/components/Popup";
import SDKManager, { Platform } from "../btoolkit/sdk/SDKManager";
import BButton from "../btoolkit/components/BButton";
import App from "../btoolkit/framework/App";
import QuLe from "../btoolkit/sdk/QuLe";

const { ccclass, property } = cc._decorator;

@ccclass
export default class PopupInvitation extends Popup {

    @property(cc.Node)
    btnShare: cc.Node = null;

    static instance: PopupInvitation = null;
    private onFinish: Function;
    private dialogFrameName: string;

    /** 弹出炫耀提示，回调 onFinish(shareSuccess: boolean) */
    static show(onFinish: Function, dialogFrameName?: string) {
        if(QuLe.instance.remoteConfig && !QuLe.instance.remoteConfig.invite){
            onFinish && onFinish(false);
            return;
        }
        if (PopupInvitation.instance) {
            onFinish && onFinish(false);
            return;
        }
        PopupInvitation.instance = cc.instantiate(App.instance.resPreloader.getRes('prefabs/common/PopupInvitation')).getComponent(PopupInvitation);
        PopupInvitation.instance.node.parent = cc.Canvas.instance.node;
        PopupInvitation.instance.dialogFrameName = dialogFrameName;
        PopupInvitation.instance.getComponent(PopupInvitation).onFinish = onFinish;
    }

    onDestroy() {
        PopupInvitation.instance = null;
        if (this.onFinish) {
            this.onFinish(false);
            this.onFinish = null;//确保只执行一次
        }
    }

    onLoad() {
        super.onLoad();

        if (this.dialogFrameName) {
            let atlas: cc.SpriteAtlas = App.instance.resPreloader.getRes('textures/common/common');
            let sprite = this.dialog.getComponent(cc.Sprite);
            sprite.spriteFrame = atlas.getSpriteFrame(this.dialogFrameName);
            sprite.sizeMode = cc.Sprite.SizeMode.TRIMMED;
        }

        this.btnShare.scale = 0;
        cc.tween(this.btnShare).delay(0.3).to(0.2, { scale: 1 }, { easing: 'backOut' }).call(() => {
            BButton.addButton(this.btnShare).setTouchUpEvent(() => {
                SDKManager.instance.share((over3Seconds: boolean) => {
                    this.node.destroy();
                    if (this.onFinish) {
                        this.onFinish(over3Seconds);
                        this.onFinish = null;//确保只执行一次
                    }
                })
            });
        }).start();

        this.btnClose.scale = 0;
        cc.tween(this.btnClose).delay(0.4).to(0.2, { scale: 1 }, { easing: 'backOut' }).start();
    }

}
